package main;
/**
 * class containing all the different resources used by the player
 * @author Bart Machielse and Justin Yu
 * no parameters
 *
 */

public class resourceArray {

	int wood;
	int metal;//same as stone
	int food;
	private int hap;
	private int res;//research per turn
	private TechTree tree;
	private double resef;//efficiency with research, not implemented yet.
	private ColonyArray Colonies = new ColonyArray();
	protected ScoreCounter score;
	int[] array = new int[5];
	private Player Owner;

	/**
	 * first basic constructor
	 * @param o owner
	 */
	public resourceArray(Player o){
		wood=100;//random base setting, change this and all others to decided values
		metal=100;
		food=100;
		hap=100;
		res=100;
		
		array[0] = wood;
		array[1] = metal;
		array[2] = food;
		array[3] = hap;
		array[4] = res;
		
		Owner=o;
		
		setResef(1);
		score=new ScoreCounter(o);
		setTree(new TechTree(o));
	}
	/**
	 * get score
	 * @return score
	 */
	public ScoreCounter getScore(){
		return score;
	}
	
	/**
	 * get array of resources
	 * @return array of resources
	 */
	public int[] getArray() {
		return array;
	}
	/**
	 * update the resource array, called after every change
	 */
	public void update() {
		array[0] = wood;
		array[1] = metal;
		array[2] = food;
		array[3] = hap;
		array[4] = res;
	}
	/**
	 * takes the array and changes the resources to match it
	 * 	 */
	public void synch(){
		wood=array[0];
		metal=array[1];
		food=array[2];
		hap=array[3];
		res=array[4];
	}
/**
 * Method to increment turn
 * @return boolean to indicate if shift was succesful, used for debugging.
 */
	public boolean incrementTurn(){
		//wood, stone, food
		tree.IncrementTurn(resef, res);
		score.IncrementTurn();
		//System.out.println(score.getScore());
		int[] nresources=Colonies.IncrementTurn();//gets the new resources for the turn
		//System.out.println("Before" + wood);
		wood+=nresources[0];
		metal+=nresources[1];
		metal+=nresources[2];
		//System.out.println("After" + wood);
		if(Owner.getPoliticalSystem().getLib()<4)
			hap+=-2;
		if(Math.abs(Owner.getPoliticalSystem().getLib()-Owner.getPoliticalSystem().getSoc())> 2)
			hap+=-2;
		if(Owner.getPoliticalSystem().getLib()>6)
			hap+=2;
		if(hap<70)
			resef*=.98;
		if(hap>130)
			resef*=1.02;
		if(hap<60){
			if(Math.random()*65>hap){
				if(Owner.getRes().getColonies().getArray().size()!=0)
					Owner.getOpponent().getRes().getColonies().getArray().add(Owner.getRes().getColonies().getArray().remove((int)Math.random()*(Owner.getRes().getColonies().getArray().size()-1)));
			}//if happiness too low then have a chance of giving a random colony to the opponent
				
		}
		update();
		return true;
	}

	/**
	 * returns wood amount
	 * @return wood
	 */
	public int getWood(){
		return wood;
	}
	
	/**
	 * @return metal
	 */
	public int getMetal() {
		return metal;
	}
	
	/**
	 * sets metal amount
	 * @param metal
	 */
	public void setMetal(int metal) {
		this.metal = metal;
		update();
	}
	
	/**
	 * @return food
	 */
	public int getFood() {
		return food;
	}
	
	/**
	 * sets food amount
	 * @param food
	 */
	public void setFood(int food) {
		this.food = food;
		update();
	}
	
	/**
	 * @return happiness
	 */
	public int getHappiness() {
		return hap;
	}
	
	/**
	 * sets happiness amount
	 * @param happiness
	 */
	public void setHappiness(int happiness) {
		this.hap = happiness;
		update();
	}
	
	/**
	 * @return research
	 */
	public int getResearch() {
		return res;
	}
	
	/**
	 * sets research amount
	 * @param research
	 */
	public void setResearch(int research) {
		this.res = research;
		update();
	}
	
	/**
	 * sets wood amount
	 * @param wood
	 */
	public void setWood(int wood) {
		this.wood = wood;
		update();
	}
	
	/**
	 * @return research
	 */
	public int getRes() {
		return res;
	}
	
	/**
	 * returns research amount
	 * @param res
	 */
	public void setRes(int res) {
		this.res = res;
		update();
	}

	/**
	 * returns tech tree
	 * @return tech tree
	 */
	public TechTree getTree() {
		return tree;
	}
	
	/**
	 * sets tech tree
	 * @param tree
	 */
	public void setTree(TechTree tree) {
		this.tree = tree;
	}
	
	/**
	 * 
	 * @return research efficiency
	 */
	public double getResef() {
		return resef;
	}
	
	/**
	 * sets research efficiency
	 * @param resef
	 */
	public void setResef(double resef) {
		this.resef = resef;
	}

	/**
	 * @return the colonies
	 */
	public ColonyArray getColonies() {
		return Colonies;
	}

	/** 
	 * @param colonies the colonies to set
	 */
	public void setColonies(ColonyArray colonies) {
		Colonies = colonies;
	}
	/**
	 * method used to upgrade units for the rest of forever
	 * @param name name of unit type to change
	 * @param stat name of stat to change
	 * @param improve amount fo improve by
	 */
	public void upgrade(String name, String stat, int improve){
		if(stat.equals("resef"))
			resef+= (double)improve*.01;
		if(stat.equals("res"))
			res+= improve;
		if(stat.equals("wood")){
			wood+=improve;
			if(stat.equals("metal"));
			metal+=improve;
		}
		if(stat.equals("food")){
			food+=improve;

		}
	}


}
